package com.xhorse.lockplatform.comm.util;


import com.xhorse.lockplatform.comm.model.BaseQuery;

/**
 * 分页器
 *
 * @author Tom
 */
public class Paginator {
    public static final int DEFAULT_SIZE = 12;
    private int start;
    private int size;
    private int page;
    /**
     * 总记录数
     */
    private int total;
    /**
     * 是否需要总记录数
     */
    private boolean needTotal;
    private boolean hasNextPage;
    private boolean hasPrePage;
    private boolean hasData;
    /**
     * 是否为分页查询(有可能用于导出全部数据)
     */
    private boolean pageless;
    private String path;
    private BaseQuery query;

    public Paginator() {
        super();
    }

    public Paginator(int page) {
        this.page = page;
        init();
    }

    public Paginator(int page, int size) {
        this.page = page;
        this.size = size;
        init();
    }

    private void init() {
        if (size < 1) {
            size = DEFAULT_SIZE;
        }
        if (page < 1) {
            page = 1;
        }
        start = (page - 1) * size;
        hasPrePage = page > 1;
    }

    public boolean isPageless() {
        return pageless;
    }

    public void setPageless(boolean pageless) {
        this.pageless = pageless;
    }

    public boolean isNeedTotal() {
        return needTotal;
    }

    public void setNeedTotal(boolean needTotal) {
        this.needTotal = needTotal;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public SqlFunc getFn() {
        return SqlFunc.get();
    }

    public BaseQuery getQuery() {
        return query;
    }

    public void setQuery(BaseQuery query) {
        this.query = query;
    }

    public boolean isHasPrePage() {
        return hasPrePage;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public boolean isHasData() {
        return hasData;
    }

    public void setHasData(boolean hasData) {
        this.hasData = hasData;
    }

    public boolean isHasNextPage() {
        return hasNextPage;
    }

    public void setHasNextPage(boolean hasNextPage) {
        this.hasNextPage = hasNextPage;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        init();
    }

    public int getStart() {
        return start;
    }

    public void setStart(int start) {
        this.start = start;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
        init();
    }

    /**
     * 每次多取一条记录用来判断是否有下一页
     *
     * @return
     */
    public int getSqlSize() {
        return size + 1;
    }
}
